<?php
/**
 * 《会议管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Visit.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Conference extends InfoUtil {
	public $tbl = Constant::tbl_conference;

	public $type = array(
		'-' => '请选择',
		'1' => '例会',
		'2' => '评审会',
		
		'3' => '培训',
		'4' => '行政',
				
		'9' => '其它',
	);
	
	public $time = array(
		'-' => '请选择',
		'1' => '上午1 07:45',
		'2' => '上午2 08:00',
		'3' => '下午1 13:30',
		'4' => '下午2 14:00',
		'5' => '晚上1 18:00',
		'6' => '晚上2 18:30',
		'7' => '晚上3 19:00',
		'8' => '晚上4 20:00',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '计划',
		'2' => '草稿',		
		'3' => '暂停',
		'4' => '停止',
		'8' => '完成',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '主题',
		'emp' => '主持人',
		't1' => '会议时间',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'emp' => '主持人',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('time_option', $this->time);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'会议信息' => array('cc'=>'info.php?do=Conference.find', 'js'=>'1'),
			'参会人员' => array('cc'=>'info.php?do=ConferenceEmp.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);
		
		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'info/conference/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'pubdate') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['pubdate'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		$emp = $_SESSION['id'];
		
		$p = array('1'=>'155', '2'=>'156', '3'=>'174');
		
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
		
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}

		return " visible = 1 AND (oper = '" . $emp . "' OR emp = '" . $emp . "')";
	}

	function _where($request) {
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
		}
		
		if (!empty($date2_s)) {
			$where .= " AND t1 < '" . $date2_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['num_'] = $this->_num($row['id'], 'all');
				$row['absent_'] = $this->_num($row['id'], '2');

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/conference/list');
		return true;
	}
	
	function _num($conference, $status) {
		require_once(dirname(__FILE__) . '/ConferenceEmp.class.php');
		$aa = new ConferenceEmp();
		
		return $aa->_num($conference, $status);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/conference/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$name = addslashes($request['name']);
		$address = addslashes($request['address']);

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$tt = $this->_tt($t1, $t2);

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, name, t1, t2, address, content, emp, oper, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $t1, $t2, $address, $content, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _date($t1, $t2) {
		$t1 = strtotime($t1);
		$t2 = strtotime($t2);
		
		$date1 = date("Y-m-d", $t1);
		$date2 = date("Y-m-d", $t2);		
		if ($date1 == $date2) {
			return sprintf("%s(%s~%s)", $date1, date("H:i", $t1), date("H:i", $t2));
		}
		
		return sprintf("%s至%s", date("Y-m-d H:i", $t1), date("Y-m-d H:i", $t2));
	}
	
	function _tt($t1, $t2) {

		return strtotime($t2) - strtotime($t1);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/conference/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$name = addslashes($request['name']);
		$address = addslashes($request['address']);

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$tt = $this->_tt($t1, $t2);

		$content = addslashes($request['content']);
		
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', name = '%s', t1 = '%s', t2 = '%s', address = '%s', content = '%s', emp = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $t1, $t2, $address, $content, $emp, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'info/conference/upload');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['num_'] = $this->_num($row['id'], 'all');
		$row['absent_'] = $this->_num($row['id'], '2');

		$row['list'] = $this->_conference_emp($row['id']);
		
		$controller->assign_by_ref('result', $row);
		
		$controller->display($request, 'info/conference/view');
		return true;
	}
	
	function _conference_emp($id, $status = 'all') {
		require_once(dirname(__FILE__) . '/ConferenceEmp.class.php');
		$aa = new ConferenceEmp();
		
		return $aa->_list($id);
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['date'] = $this->_date($row['t1'], $row['t2']);
		
		$row['files'] = $this->_files($row['id']);
		
		$row = $this->_view_t($row);
		
		return $row;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);
		
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->_view($id);
			if (!$this->_is_delete($row)) {
				continue;
			}

			$this->_delete3($id);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _delete3($id) {
		$db = newdb($this->dsn_w);
		if ($this->_is_admin()) {
			$fmt = "DELETE FROM %s WHERE id = '%s'";
		} else {
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
		}
		
		$sql = sprintf($fmt, $this->tbl, $id);
		debug($sql);
		$db->query($sql);
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function verify($controller, $request) {		
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('conference', $row);
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/conference/verify');
		return true;
	}

	function verifyPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		// $this->_status($id, $status);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function batch_verify($controller, $request) {
		$this->pretreat($controller);
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id %s";
		$sql = sprintf($fmt, $this->tbl, $insql);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
			
			$data[] = $row;
		}
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'finance/expense/batch_verify');
		return true;
	}
	
	function _batch_insql222($a) {
		// $a = json_decode($cc);		
		if (!$a || $a == NULL) {
			debug("cc is NULL");
			return '';
		}
		
		$insql = "in ('000000000";
		while (list($k, $v) = each($a)) {
			$insql .= "', '" . $v;
		}
		$insql .= "')";
		
		return $insql;
	}
	
	function batch_verifyPost($controller, $request) {
		$this->pretreat($controller);
		
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request['cc'])) {
			$fmt = "UPDATE %s SET content = concat(content, '%s'), status = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $content, $status, $id);
			debug($sql);
			$db->query($sql);
			
			$this->_status($id, $status);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function notice($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['num_'] = $this->_num($row['id'], 'all');
		$row['absent_'] = $this->_num($row['id'], '2');

		$controller->assign_by_ref('conference', $row);
		
		require_once(dirname(__FILE__) . '/ConferenceEmp.class.php');
		$aa = new ConferenceEmp();
		
		$list = $aa->_list2($id);
		
		$controller->assign_by_ref('result', $list);
		$controller->assign_by_ref('result_all', $list);
		
		$controller->display($request, 'info/conference/notice');
		return true;
	}
		
	function noticePost($controller, $request) {
		$this->pretreat($controller);
		
		$list = $request['list'];
		if (empty($list)) {
			return false;
		}
		
		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['time'] = (strtotime($row['t1']) > time()) ? "1" : "2";
				
		$row['list'] = $this->_conference_emp($row['id']);
		
		$row['extra'] = addslashes($request['content']);

		$from = $_SESSION['id'];
		
		$bb = explode(',', $list);
		while (list($k, $to) = each($bb)) {
			// $to = "101";
			$row['to'] = Name::emp($to);
	
			$controller->assign_by_ref('result', $row);
			
			$subject = sprintf('会议 %s', $row['id']);
			$body = $controller->sunxy($request, 'info/conference/body_1');
			// debug($body);
						
			$this->_notice($from, $to, $subject, $body, 1);
		}
		
		return true;
	}
	
	function ajax_oper($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
		
		if ($this->_ajax_x($id, 'oper')) {
			$ret['status'] = 1;
		} else if ($this->_ajax_x($id, 'emp')) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}

		echo json_encode($ret);
	}
	
	function tj1($controller, $request) {
		$a = array(
			'南山会议'=>array('a'=>'344', 'office'=>'1'),
			'沙井会议'=>array('a'=>'344', 'office'=>'2'),
		);
	
		while (list($k, $v) = each($a)) {
			$request['title'] = $k;
			$request['a'] = $v['a'];
			$request['office'] = $v['office'];
	
			$this->tj($controller, $request);
		}
	}
	
	function tj($controller, $request) {
		// $bb = $this->_list_email('300');
		$bb = $this->_list_email($request['a']);
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
	
		$from = $this->email;
		$subject = $request['title'];		
		$office = $request['office'];
		
		$d2 = $this->_tj_df("createtime", "%Y-%m-%d");
		$d3 = $this->_tj_df("modifytime", "%Y-%m-%d");
		
		$dd = $this->_tj_df_not("%Y-%m-%d");
			
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE visible = 1 AND office = '%s' AND %s ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $office, $d2);
		// $sql = sprintf("SELECT * FROM %s ORDER BY id DESC LIMIT 10", $this->tbl);
		// echo $sql . "\n";
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
				
			$row['num_'] = $this->_num($row['id'], 'all');
			$row['absent_'] = $this->_num($row['id'], '2');
				
			$row['list'] = $this->_conference_emp($row['id']);
			
			$controller->assign_by_ref('result', $row);			
			
			$subject = sprintf("%s[%s,%s]", $row['name'], $row['emp_t'], $row['date']);
			
			$body = $this->_tj_html($controller, $request, 'info/conference/tj_list');
			
			while (list($k, $v) = each($row['list'])) {
				$this->_notice($row['oper'], $v['emp'], $subject, $body, 1);
			}
			
			$this->_tj_mail($from, $bb, $subject, $body);
		
			$data[] = $row;
		}	
	
		logdate($controller, $request);
		return true;
	}
}
?>
